package helperDao

import (
	"fmt"
	"gitee.com/yangguoliang001/go-gin-vue3/BO"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/vo"
)

func (h helperDao) QueryTables(dbName string) (tables []BO.DbTablesBO, err error) {
	// 在 MySQL 的console 中执行查询时，显示的列为 TABLE_NAME 大写，无法实现 转换，所以需要 改为 table_name，才能和 BO.DbTablesBO 中的类型（tableName）实现转化
	sql := fmt.Sprintf("SELECT TABLE_NAME as table_name, TABLE_COMMENT as table_comment FROM `information_schema`.`tables` WHERE `table_schema`= '%s'", dbName)
	err = h.db.Debug().Raw(sql).Scan(&tables).Error
	return tables, err
}

func (h helperDao) QueryTableColumns(req QO.SearchMysqlReq) (columnsRes []BO.TableColBO, err error) {

	sqlTableColumn := fmt.Sprintf("SELECT COLUMN_NAME as column_name, COLUMN_COMMENT as column_comment FROM `information_schema`.`columns` WHERE `table_schema`= '%s' AND `table_name`= '%s' ORDER BY `ORDINAL_POSITION` ASC",
		req.DbName, req.TableName)
	err = h.db.Debug().Raw(sqlTableColumn).Scan(&columnsRes).Error
	return columnsRes, err

}

func (h helperDao) QueryTableData(sql string) (searchRes vo.SearchMysqlDataVO, err error) {
	row := h.db.Debug().Raw(sql)
	err = row.Scan(&searchRes.List).Error
	return searchRes, err
}
